package edu.neusoft.yuejian_09.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.adapter.ShowListAdapter;
import edu.neusoft.yuejian_09.base.Constant;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.bean.Collection;
import edu.neusoft.yuejian_09.bean.Show;
import edu.neusoft.yuejian_09.bean.User;
import edu.neusoft.yuejian_09.model.CollectModel;
import edu.neusoft.yuejian_09.model.ShowModel;
import edu.neusoft.yuejian_09.model.UserModel;
import it.sephiroth.android.library.picasso.Picasso;

import static edu.neusoft.yuejian_09.base.Constant.PHOTO_URL;

public class UserInfoActivity extends Activity implements RetrofitListener {

    private ImageView u_pic,img_return,img_collect;
    private TextView u_name,u_age,u_intro,u_sex;

    private RecyclerView recyclerView;

    private int user_id;
    private String user_name;
    private String user_pic;
    private String user_intro;
    private String user_sex;
    private int user_age;
    private int u_id;

    private Collection collectResult;
    private String coresult="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_user_info);
        initViews();
        initEvents();
        initData();
    }

    public void initViews(){
        u_name=(TextView)findViewById(R.id.username);
        u_intro=(TextView)findViewById(R.id.userintro);
        u_sex=(TextView)findViewById(R.id.sex);
        u_age=(TextView)findViewById(R.id.age);
        img_return=(ImageView)findViewById(R.id.img_return);
        img_collect=(ImageView) findViewById(R.id.collect);
        u_pic=(ImageView)findViewById(R.id.userpic);
        recyclerView= (RecyclerView)findViewById(R.id.rv_usershows);

    }

    public void initEvents(){
        img_return.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_collect.setOnClickListener(new View.OnClickListener() {//收藏
            @Override
            public void onClick(View v) {
                collectUser();
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

    }

    public void initData(){

        getUserInfo();
        getShowsList();
        getUserId();//获取登录用户的Id
        //checkCollect();
    }

    //获取用户信息数据
    public void getUserInfo(){

        Bundle bundle = getIntent().getExtras();
        user_id = bundle.getInt("user_id");
        System.out.printf("user_id");
        UserModel userModel = new UserModel();
        userModel.getUserById(user_id,this);

    }
    //获取晒图列表数据
    public void getShowsList(){
        ShowModel showModel=new ShowModel();
        showModel.getShowByUser(user_id,this);
    }

    //获取个人用户信息
    public void getUserId(){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sharedPreferences.getInt("user_id", 0);
    }

    //收藏活动
    private void collectUser( ){
        CollectModel collectModel = new CollectModel();
        collectModel.collectUser(u_id,user_id,this);
    }

    //检查收藏状态
    private void checkCollect(){
        CollectModel collectModel = new CollectModel();
        collectModel.isCollected(u_id,user_id,0,this);
    }

    private void refresh(){
        checkCollect();
    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag){
            case Constant.USERBYID:
                User user=(User)o;
                user_name = user.getUser_name();
                user_pic =PHOTO_URL+user.getUser_pic();
                user_age = user.getUser_age();
                user_intro=user.getUser_intro();
                user_sex=user.getUser_sex();
                u_name.setText(user_name);
                u_intro.setText(user_intro);
                u_age.setText(user_age+" ");
                u_sex.setText(user_sex);
                //t_ask.setText(train_phone+"");
                if (user_pic != "") {
                    Picasso.with(UserInfoActivity.this)
                            .load(user_pic)
                            .into(u_pic);
                    u_pic.setScaleType(ImageView.ScaleType.FIT_XY);
                } else {
                    u_pic.setImageResource(R.drawable.no_userpic);
                }
                break;
            case Constant.SHOWBYUSER:
                List<Show> data=(List<Show>)o;
                recyclerView.setAdapter(new ShowListAdapter(UserInfoActivity.this,data));
                break;
            case Constant.COLLECTUSER:
                Collection collection=(Collection)o;

                if ("yes".equals(coresult)){
                    Toast.makeText(UserInfoActivity.this,"取消收藏成功！",Toast.LENGTH_SHORT).show();
                    img_collect.setImageResource(R.drawable.ic_collect);
                }else{
                    Toast.makeText(UserInfoActivity.this,"收藏成功！",Toast.LENGTH_SHORT).show();
                    img_collect.setImageResource(R.drawable.ic_collect1);
                }
                refresh();
                break;
            case Constant.COLLECTSTATE:
                Collection collectstate=(Collection)o;

                String result = collectstate.getCollected();
                System.out.printf(result);
                if ("0".equals(result)){
                    img_collect.setImageResource(R.drawable.ic_collect);
                    coresult = "no";
                }else {
                    coresult = "yes";
                    img_collect.setImageResource(R.drawable.ic_collect1);
                }

                break;
        }


    }
    @Override
    public void onFail(){ Toast.makeText(UserInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}


    /*@Override
    public void onSuccess(User user,int flag){
        user_name = user.getUser_name();
        user_pic =PHOTO_URL+user.getUser_pic();
        user_age = user.getUser_age();
        user_intro=user.getUser_intro();
        user_sex=user.getUser_sex();
        u_name.setText(user_name);
        u_intro.setText(user_intro);
        u_age.setText(user_age+" ");
        u_sex.setText(user_sex);
        //t_ask.setText(train_phone+"");
        if (user_pic != "") {
            Picasso.with(UserInfoActivity.this)
                    .load(user_pic)
                    .into(u_pic);
            u_pic.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            u_pic.setImageResource(R.drawable.no_userpic);
        }

    }
    @Override
    public void onFail(){ Toast.makeText(UserInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}*/

    /*@Override
    public void onSuccessTow(List<Show> data,int flag ){

        //System.out.printf("data");
        recyclerView.setAdapter(new ShowListAdapter(UserInfoActivity.this,data));
    }

    @Override
    public void onFailTow(){ Toast.makeText(UserInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}

    //获取个人用户信息
    public void getUserId(){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sharedPreferences.getInt("user_id", 0);
    }*/
    //收藏比赛
    /*private void collectUser( ){
        CollectModel collectModel = new CollectModel();
        Call call = collectModel.collectUser(u_id,user_id);
        call.enqueue(new Callback<Collection>() {
            @Override
            public void onResponse(Call<Collection> call, Response<Collection> response) {
                if ("yes".equals(coresult)){
                    Toast.makeText(UserInfoActivity.this,"取消收藏成功！",Toast.LENGTH_SHORT).show();
                    img_collect.setImageResource(R.drawable.ic_collect);
                }else{
                    Toast.makeText(UserInfoActivity.this,"收藏成功！",Toast.LENGTH_SHORT).show();
                    img_collect.setImageResource(R.drawable.ic_collect1);
                }
                refresh();
            }

            @Override
            public void onFailure(Call<Collection> call, Throwable t) {
                Toast.makeText(UserInfoActivity.this,"fail",Toast.LENGTH_SHORT).show();
            }
        });
    }*/

   /* private void checkCollect(){
        CollectModel collectModel = new CollectModel();
        Call collectResultCall = collectModel.isCollected(u_id,user_id,0);

        collectResultCall.enqueue(new Callback<Collection>() {
            @Override
            public void onResponse(Call<Collection> call, Response<Collection> response) {
                collectResult = response.body();
                String result = collectResult.getCollected();
                if ("0".equals(result)){
                    img_collect.setImageResource(R.drawable.ic_collect);
                    coresult = "no";
                }else {
                    coresult = "yes";
                    img_collect.setImageResource(R.drawable.ic_collect1);
                }
            }

            @Override
            public void onFailure(Call<Collection> call, Throwable t) {
                Toast.makeText(UserInfoActivity.this,"fail",Toast.LENGTH_SHORT).show();
            }
        });

    }*/



}
